package com.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.course.bean.Directory;
import com.example.course.converter.DirectoryConverter;
import com.example.course.dto.DirectoryDTO;
import com.example.course.service.DirectoryService;
import com.example.course.mapper.DirectoryMapper;
import com.example.course.vo.DirectoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Jasmine-drs
 * @description 针对表【directory(课程目录表)】的数据库操作Service实现
 * @createDate 2025-04-22 15:36:40
 */
@Service
public class DirectoryServiceImpl extends ServiceImpl<DirectoryMapper, Directory>
		implements DirectoryService {

	@Autowired
	private DirectoryConverter directoryConverter;

	@Override
	public DirectoryVO addDirectory(DirectoryDTO directoryDTO, boolean isTeacher) {
		if (!isTeacher) {
			throw new RuntimeException("只有教师才能添加课程目录");
		}

		Directory directory = directoryConverter.toEntity(directoryDTO);
		this.save(directory);
		return directoryConverter.toVO(directory);
	}

	@Override
	public List<DirectoryVO> getDirectoryByCourseId(Integer courseId) {
		LambdaQueryWrapper<Directory> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Directory::getCourseId, courseId);
		List<Directory> directoryList = this.list(queryWrapper);
		return directoryConverter.toVOList(directoryList);
	}

	@Override
	public DirectoryVO getDirectoryById(Integer id) {
		Directory directory = this.getById(id);
		return directoryConverter.toVO(directory);
	}
}




